<?php
/**
 * Created by PhpStorm.
 * @author: 汤文辉
 * @since: 2017/6/18 18:45
 * @revised Liu Shijie 2017-6-20 23:13:45
 */
include "../config/mysqlconfig.php";
class Comment{
    function __construct()
    {
        global $con;
        $this->con = $con;
    }

    /**
     * 用户发表评论，将评论相关信息插入数据库
     * @param $book_id
     * @param $text
     * @param $time
     * @param $user_id
     * @return int
     */
    function Remark($book_id,$text,$time,$user_id){
        if(!is_null($text)){

		     do{
		         $com_id = rand(20340,21000);
		         $sql0 = "select * from comment where comment_id = $com_id;";
		         $s0 = mysqli_query($this->con,$sql0);
		     }while ( mysqli_num_rows($s0) >= 1);

            $sql = "insert into comment(comment_id,comment_time,comment_content) values($com_id,'$time','$text');";
            $s1 = mysqli_query($this->con,$sql);
            $sql = "insert into book_user_comment(comment_id,book_id,user_id) values($com_id,$book_id,'$user_id');";
            $s2  = mysqli_query($this->con,$sql);

            if ($s1 && $s2){
                $result = 1;  //评论成功
            }else{
                $result = 0;  //评价失败
            }
        }else{
            $result = 2;   //评论为空
        }
        return $result;
    }

}